package com.zyq.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyq.annotation.Log;
import com.zyq.annotation.PreAuthorize;
import com.zyq.entity.SysLog;
import com.zyq.service.ILogService;
import com.zyq.util.PageResult;
import com.zyq.util.R;
import com.zyq.vo.req.LogPageReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 日志管理
 */
@RestController
@RequestMapping("/logs")
public class LogController extends BaseController{

    @Autowired
    private ILogService logService;

    /**
     * 分页查找操作日志
     * @param reqVo
     * @return
     */
    @PreAuthorize(authorize = "sys:log:list")
    @PostMapping("/page")
    public R<PageResult<SysLog>> pageInfo(@RequestBody LogPageReqVo reqVo){
        QueryWrapper<SysLog> query = Wrappers.query();
        query.lambda().orderByDesc(SysLog::getCreateTime);
        PageInfo<SysLog> pageInfo = PageHelper.startPage(reqVo.getPageNum(), reqVo.getPageSize()).doSelectPageInfo(() -> logService.list(query));
        return R.ok(new PageResult<>(pageInfo));
    }

    @PreAuthorize(authorize = "sys:log:delete")
    @Log(module = "系统管理-日志管理",action = "删除日志接口")
    @PostMapping("/delete")
    public R<String> deletedLog(@RequestBody List<String> logIds){
        if (logService.removeByIds(logIds)){
            return R.ok();
        }
        return R.error();
    }
}
